package com.zcmob.web.admanagement.vo;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;

public class Keys {

    private String waps_key;

    private String miidi_id;
    private String miidi_key;

    private String dianjin_id;
    private String dianjin_key;

    private String qumi_id;
    private String qumi_key;

    private String adview_key;

    public String getWaps_key() {
        return waps_key;
    }

    public void setWaps_key(String waps_key) {
        this.waps_key = waps_key;
    }

    public String getMiidi_id() {
        return miidi_id;
    }

    public void setMiidi_id(String miidi_id) {
        this.miidi_id = miidi_id;
    }

    public String getMiidi_key() {
        return miidi_key;
    }

    public void setMiidi_key(String miidi_key) {
        this.miidi_key = miidi_key;
    }

    public String getDianjin_id() {
        return dianjin_id;
    }

    public void setDianjin_id(String dianjin_id) {
        this.dianjin_id = dianjin_id;
    }

    public String getDianjin_key() {
        return dianjin_key;
    }

    public void setDianjin_key(String dianjin_key) {
        this.dianjin_key = dianjin_key;
    }

    public String getQumi_id() {
        return qumi_id;
    }

    public void setQumi_id(String qumi_id) {
        this.qumi_id = qumi_id;
    }

    public String getQumi_key() {
        return qumi_key;
    }

    public void setQumi_key(String qumi_key) {
        this.qumi_key = qumi_key;
    }

    public String getAdview_key() {
        return adview_key;
    }

    public void setAdview_key(String adview_key) {
        this.adview_key = adview_key;
    }

    public static void main(String[] args) {
        String rootpath = "D:\\batchapks\\apks\\";
        String packagename = "com.fingersoft.hillclimb";
        String tmpPath = rootpath + packagename + "_tmp";
        String targetPath = rootpath + packagename;
        String tmpPublicFile = tmpPath + "\\res\\values\\public.xml";
        String targetPublicFile = targetPath + "\\res\\values\\public.xml";
        modifyPublicFile(packagename, tmpPublicFile, targetPublicFile);
    }

    private static void modifyPublicFile(String packagename, String tmpPublicFile, String targetPublicFile) {

        try {
            BufferedReader br = new BufferedReader(new FileReader(tmpPublicFile));
            BufferedWriter bw = new BufferedWriter(new FileWriter(targetPublicFile));
            int maxLayoutId = 0;
            int maxidId = 0;

            int maxPreId = 0;// 如果public中没有layout或者id选项时，用于构建

            StringBuilder layoutOutput = new StringBuilder();
            StringBuilder idOutput = new StringBuilder();

            // TODO
            String line = null;
            while ((line = br.readLine()) != null) {

                /*
                 * for id
                 * 
                 * textView imageView tlayout imageView1 title textView1 imgb
                 * btnAction
                 */
                if (line.contains("<public type=\"drawable\"")) {
                    // 判断是否对maxPreId赋值
                    String tmp = line.substring(line.indexOf("id=\"0x7f") + 8, line.length());
                    String preid = tmp.substring(0, tmp.indexOf("\"") - 4);
                    if (Integer.valueOf(preid).intValue() > maxPreId) {
                        maxPreId = Integer.valueOf(preid).intValue();
                    }
                } else if (line.contains("<public type=\"layout\"")) {
                    String tmp = line.substring(line.indexOf("id=\"0x") + 6, line.length());
                    String layoutId = tmp.substring(0, tmp.indexOf("\""));
                    if (Integer.parseInt(layoutId, 16) > maxLayoutId) {
                        maxLayoutId = Integer.parseInt(layoutId, 16);
                    }
                } else if (line.contains("<public type=\"id\"")) {
                    String tmp = line.substring(line.indexOf("id=\"0x") + 6, line.length());
                    String idid = tmp.substring(0, tmp.indexOf("\""));
                    if (Integer.parseInt(idid, 16) > maxidId) {
                        maxidId = Integer.parseInt(idid, 16);
                    }
                } else if (line.contains("</resources>")) {
                    if (maxLayoutId > 0) {
                        
                        layoutOutput.append("<public type=\"layout\" name=\"airpush_notify\" id=\"0x")
                        .append(Integer.toHexString(maxLayoutId + 1)).append("\" />\n");
                        
                    } else {
                        layoutOutput.append("<public type=\"layout\" name=\"airpush_notify\" id=\"")
                        .append("0x7f").append(maxPreId < 10 ? ("0" + maxPreId) : maxPreId).append("0000").append("\" />\n");
                    }
                    
                    if (maxidId > 0) {
                        layoutOutput.append("<public type=\"id\" name=\"textView\" id=\"0x")
                        .append(Integer.toHexString(maxidId + 1)).append("\" />\n");
                        layoutOutput.append("<public type=\"id\" name=\"imageView\" id=\"0x")
                        .append(Integer.toHexString(maxidId + 2)).append("\" />\n");
                        layoutOutput.append("<public type=\"id\" name=\"tlayout\" id=\"0x")
                        .append(Integer.toHexString(maxidId + 3)).append("\" />\n");
                        layoutOutput.append("<public type=\"id\" name=\"imageView1\" id=\"0x")
                        .append(Integer.toHexString(maxidId + 4)).append("\" />\n");
                        layoutOutput.append("<public type=\"id\" name=\"title\" id=\"0x")
                        .append(Integer.toHexString(maxidId + 5)).append("\" />\n");
                        layoutOutput.append("<public type=\"id\" name=\"textView1\" id=\"0x")
                        .append(Integer.toHexString(maxidId + 6)).append("\" />\n");
                        layoutOutput.append("<public type=\"id\" name=\"imgb\" id=\"0x")
                        .append(Integer.toHexString(maxidId + 7)).append("\" />\n");
                        layoutOutput.append("<public type=\"id\" name=\"btnAction\" id=\"0x")
                        .append(Integer.toHexString(maxidId + 8)).append("\" />\n");
                        
                    } else {
                        idOutput.append("<public type=\"id\" name=\"textView\" id=\"")
                        .append("0x7f").append(maxPreId < 9 ? ("0" + maxPreId + 1) : maxPreId + 1).append("0000").append("\" />\n");
                        idOutput.append("<public type=\"id\" name=\"imageView\" id=\"")
                        .append("0x7f").append(maxPreId < 9 ? ("0" + maxPreId + 1) : maxPreId + 1).append("0001").append("\" />\n");
                        idOutput.append("<public type=\"id\" name=\"tlayout\" id=\"")
                        .append("0x7f").append(maxPreId < 9 ? ("0" + maxPreId + 1) : maxPreId + 1).append("0002").append("\" />\n");
                        idOutput.append("<public type=\"id\" name=\"imageView1\" id=\"")
                        .append("0x7f").append(maxPreId < 9 ? ("0" + maxPreId + 1) : maxPreId + 1).append("0003").append("\" />\n");
                        idOutput.append("<public type=\"id\" name=\"title\" id=\"")
                        .append("0x7f").append(maxPreId < 9 ? ("0" + maxPreId + 1) : maxPreId + 1).append("0004").append("\" />\n");
                        idOutput.append("<public type=\"id\" name=\"textView1\" id=\"")
                        .append("0x7f").append(maxPreId < 9 ? ("0" + maxPreId + 1) : maxPreId + 1).append("0005").append("\" />\n");
                        idOutput.append("<public type=\"id\" name=\"imgb\" id=\"")
                        .append("0x7f").append(maxPreId < 9 ? ("0" + maxPreId + 1) : maxPreId + 1).append("0006").append("\" />\n");
                        idOutput.append("<public type=\"id\" name=\"btnAction\" id=\"")
                        .append("0x7f").append(maxPreId < 9 ? ("0" + maxPreId + 1) : maxPreId + 1).append("0007").append("\" />\n");
                    }

                    line = layoutOutput.toString() + idOutput.toString() + "</resources>";
                    
                }

                bw.write(line + "\n");
                bw.flush();
            }
            
            br.close();
            bw.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
